package com.scu.gkvr_system_backend.config;

import com.scu.gkvr_system_backend.mapper.GsLiScoreRankMapper;
import com.scu.gkvr_system_backend.mapper.GsWenScoreRankMapper;
import com.scu.gkvr_system_backend.pojo.ScoreRank;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@Slf4j
public class DataEngine implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private GsWenScoreRankMapper gsWenScoreRankMapper;
    @Autowired
    private GsLiScoreRankMapper gsLiScoreRankMapper;

    private   static Map<Integer, Integer> li;
    private  static Map<Integer, Integer> wen;

    public static  Integer searchRank(int score,String type){
        Map<Integer, Integer> temp = "文科".equals(type)?wen:li;
        Integer i = temp.get(score);
        return i==null?1:i;

    }


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null) {
            // 确保只在最顶层容器初始化完成后执行一次
            List<ScoreRank> wenRanks = gsWenScoreRankMapper.findAll();
            wen = getDataSets(wenRanks);

            List<ScoreRank> liRanks = gsLiScoreRankMapper.findAll();
           li = getDataSets(liRanks);

        }
    }

    //数据整理 得到规范的数据集
    private   Map<Integer,Integer> getDataSets(List<ScoreRank> list) {

        List<Map<Integer, List<Integer>>> sumList = new ArrayList<>();
        list.forEach(i -> {
            ArrayList<Integer> ranks = new ArrayList<>();
            if (i.getRanking2019() != null)
                ranks.add(i.getRanking2019());
            if (i.getRanking2020() != null)
                ranks.add(i.getRanking2020());
            if (i.getRanking2021() != null)
                ranks.add(i.getRanking2021());
            if (i.getRanking2022() != null)
                ranks.add(i.getRanking2022());
            if (i.getRanking2023() != null)
                ranks.add(i.getRanking2023());
            if (!ranks.isEmpty()) {
                Collections.sort(ranks);
                sumList.add(Map.of(i.getScore(), ranks));
            }
        });

        TreeMap<Integer, Integer> map = new TreeMap<>((i1, i2) -> i2.intValue() - i1.intValue());

        @Data
         class Temp {
            private int score;
            private int newRank;
        }
        sumList.stream().map(i -> {

            Temp t = new Temp();
            i.forEach((k, v) -> {
                int max = v.get(v.size()-1);
                int min = v.get(0);
                int sum = 0;
                for (Integer integer : v) {
                    sum += integer;
                }
                int avg = sum / v.size();
                t.setNewRank((int) (Math.sqrt(max - min) + avg));
                t.setScore(k);
            });
            return t;
        }).forEach(i->{
            map.put(i.getScore(),i.getNewRank());
     });
        return  map;
    }


}

